{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pregnants_tfidf</th>\n",
       "      <th>Plasma_glucose_concentration_tfidf</th>\n",
       "      <th>blood_pressure_tfidf</th>\n",
       "      <th>Triceps_skin_fold_thickness_tfidf</th>\n",
       "      <th>serum_insulin_tfidf</th>\n",
       "      <th>BMI_tfidf</th>\n",
       "      <th>Diabetes_pedigree_function_tfidf</th>\n",
       "      <th>Age_tfidf</th>\n",
       "      <th>Target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.293647</td>\n",
       "      <td>0.389263</td>\n",
       "      <td>0.068664</td>\n",
       "      <td>0.416311</td>\n",
       "      <td>-0.317941</td>\n",
       "      <td>0.093614</td>\n",
       "      <td>0.214973</td>\n",
       "      <td>0.654334</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.458093</td>\n",
       "      <td>-0.609101</td>\n",
       "      <td>-0.087047</td>\n",
       "      <td>0.287852</td>\n",
       "      <td>-0.375682</td>\n",
       "      <td>-0.371091</td>\n",
       "      <td>-0.197934</td>\n",
       "      <td>-0.103381</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.408951</td>\n",
       "      <td>0.644218</td>\n",
       "      <td>-0.087479</td>\n",
       "      <td>-0.426959</td>\n",
       "      <td>-0.229648</td>\n",
       "      <td>-0.365657</td>\n",
       "      <td>0.200318</td>\n",
       "      <td>-0.034994</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.425010</td>\n",
       "      <td>-0.502137</td>\n",
       "      <td>-0.080761</td>\n",
       "      <td>0.077736</td>\n",
       "      <td>0.062026</td>\n",
       "      <td>-0.248523</td>\n",
       "      <td>-0.463179</td>\n",
       "      <td>-0.523940</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.186954</td>\n",
       "      <td>0.082528</td>\n",
       "      <td>-0.246360</td>\n",
       "      <td>0.148546</td>\n",
       "      <td>0.125389</td>\n",
       "      <td>0.230816</td>\n",
       "      <td>0.898036</td>\n",
       "      <td>-0.003356</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   pregnants_tfidf  Plasma_glucose_concentration_tfidf  blood_pressure_tfidf  \\\n",
       "0         0.293647                            0.389263              0.068664   \n",
       "1        -0.458093                           -0.609101             -0.087047   \n",
       "2         0.408951                            0.644218             -0.087479   \n",
       "3        -0.425010                           -0.502137             -0.080761   \n",
       "4        -0.186954                            0.082528             -0.246360   \n",
       "\n",
       "   Triceps_skin_fold_thickness_tfidf  serum_insulin_tfidf  BMI_tfidf  \\\n",
       "0                           0.416311            -0.317941   0.093614   \n",
       "1                           0.287852            -0.375682  -0.371091   \n",
       "2                          -0.426959            -0.229648  -0.365657   \n",
       "3                           0.077736             0.062026  -0.248523   \n",
       "4                           0.148546             0.125389   0.230816   \n",
       "\n",
       "   Diabetes_pedigree_function_tfidf  Age_tfidf  Target  \n",
       "0                          0.214973   0.654334       1  \n",
       "1                         -0.197934  -0.103381       0  \n",
       "2                          0.200318  -0.034994       1  \n",
       "3                         -0.463179  -0.523940       0  \n",
       "4                          0.898036  -0.003356       1  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv(\"./data/FE_diabetes-tfidf.csv\")\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_train = train['Target']   \n",
    "X_train = train.drop([\"Target\"], axis=1)\n",
    "\n",
    "feat_names = X_train.columns \n",
    "\n",
    "from scipy.sparse import csr_matrix\n",
    "X_train = csr_matrix(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "lr = LogisticRegression()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('logloss of each fold is: ', array([0.49486178, 0.49963072, 0.48419016, 0.4297871 , 0.46623298]))\n",
      "('cv logloss is:', 0.4749405462792729)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/ys/anaconda3/lib/python2.7/site-packages/sklearn/linear_model/logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.\n",
      "  FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import cross_val_score\n",
    "loss = cross_val_score(lr, X_train, y_train, cv=5, scoring='neg_log_loss')\n",
    "#%timeit loss_sparse = cross_val_score(lr, X_train_sparse, y_train, cv=3, scoring='neg_log_loss')\n",
    "print ('logloss of each fold is: ',-loss)\n",
    "print ('cv logloss is:', -loss.mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, error_score='raise-deprecating',\n",
       "       estimator=LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n",
       "          intercept_scaling=1, max_iter=100, multi_class='warn',\n",
       "          n_jobs=None, penalty='l2', random_state=None, solver='liblinear',\n",
       "          tol=0.0001, verbose=0, warm_start=False),\n",
       "       fit_params=None, iid='warn', n_jobs=4,\n",
       "       param_grid={'penalty': ['l1', 'l2'], 'C': [0.1, 1, 10, 100, 1000]},\n",
       "       pre_dispatch='2*n_jobs', refit=True, return_train_score='warn',\n",
       "       scoring='neg_log_loss', verbose=0)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "penaltys = ['l1','l2']\n",
    "Cs = [ 0.1, 1, 10, 100, 1000]\n",
    "tuned_parameters = dict(penalty = penaltys, C = Cs)\n",
    "\n",
    "lr_penalty= LogisticRegression(solver='liblinear')\n",
    "grid= GridSearchCV(lr_penalty, tuned_parameters,cv=5, scoring='neg_log_loss',n_jobs = 4,)\n",
    "grid.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5099136382941524\n",
      "{'penalty': 'l1', 'C': 1}\n"
     ]
    }
   ],
   "source": [
    "# examine the best model\n",
    "print(-grid.best_score_)\n",
    "print(grid.best_params_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYsAAAEKCAYAAADjDHn2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAIABJREFUeJzt3Xl8FeXZ//HPdUIgICAIUVkNUmrLoggRwa0uRbEqtAKCC0tdwFZra/vog13wEX9ara2P7VOqIKJYrYCgFjGKloorIAFBNhekLkFUBEFlC5Dr98dM4BCSzAlkckLyfb9e88rMPfc9c50hORcz98w95u6IiIiUJ5HuAEREpPpTshARkUhKFiIiEknJQkREIilZiIhIJCULERGJpGQhIiKRlCxERCSSkoWIiESqk+4AKkvz5s09Jycn3WGIiBxUFi5c+IW7Z0fVqzHJIicnh/z8/HSHISJyUDGzD1Opp8tQIiISSclCREQiKVmIiEikGtNnISKSbMeOHRQUFLBt27Z0h1ItZGVl0bp1azIzM/ervZKFiNRIBQUFNGrUiJycHMws3eGklbuzfv16CgoKaNeu3X5tQ5ehRKRG2rZtG82aNav1iQLAzGjWrNkBnWUpWYhIjaVEsceBHgslCxGR0KBxcxk0bm66w6iWlCzQL4iIxKNhw4a75/v06UOTJk04//zzS617zTXX0LVrVzp27Ej9+vXp2rUrXbt2Zdq0aRXa56JFi3juuecOKO7SqINbRKQK3HDDDWzZsoVx48aVun7s2LEAfPDBB5x//vksXrx4v/azaNEili1bRp8+ffY71tLozEJEpAqcddZZNGrUaL/avvfee5xzzjl0796d0047jXfffReAyZMn07lzZ4477jjOOOMMtm7dypgxY3j00Uf366ykPLGeWZhZH+DPQAYwwd3vKLF+OHAXsCYs+qu7TzCzrsC9QGNgF3Cbu0+JM1YRqblueXo5Kz75KrLeirVBnVQuS3ds2ZibL+h0wLGlYsSIEUyYMIH27dvz2muvce211/L8889zyy23MGfOHI444gg2btxI/fr1GT16NMuWLeOee+6p1BhiSxZmlgGMBXoDBcACM5vh7itKVJ3i7teWKNsCDHX398ysJbDQzGa5+8a44hURqY42btzIvHnz6N+//+6ynTt3AnDyySczdOhQBg4cyIUXXhhrHHGeWfQAVrn7agAzmwz0A0omi324+7tJ85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gL7Ah/t64ArgauDtcbMDaMeyll3IWXhriuTTpe84CTqiiuUwg6aN8CFofTD9J9zFKMq8qPGXAs8GYY1zJgdFheJX+PeoJbREQi6TKUiIhEUrIQEZFIShYiIhJJyUJERCIpWYiISCQlC5EKMLNvomuV235a+NQ9ZtbQzMaZ2ftmttDM5pjZiWZW18xeDh+qEqkWlCxEqkg4flCGu68OiyYQDPDXwd27Az8GmnswQOJsYFB6IhXZl5KFyH4Inyi+y8yWmdlSMxsUlifCoR/etuB9JHlmNiBsdinhE/lm1h44EfituxcBuPt/3P2ZsO5TYX2RakGnuSL750KgK3Ac0BxYYGYvEwy9kgN0BA4nGGpkYtjmZIKnqQE6AYs9GBiuNMuAE2KJXGQ/6MxCZP+cQjCy7S53/wx4ieDL/RTgcXcvcvdPCYYbKdYCWJfKxsMkUmhmjSo5bpH9omQhUnW2EozdA8G4QseZWUY59esB22KPSiQFShYi++cVYFD4MppsgleXvgG8RvDipYSZHUHw+s1iK4FvAXjw7pF84Jak0UtzzOy8cL4Z8IW776iqDyRSHiULkf3zJMHon0uAfwM3hpedphOM7LoCeITgJTmbwjbPsHfyuJLgTYirzGwZ8BB73l9yRlhfpFrQqLMilczMGnrwBrVmBGcbJ7v7p+E7CF4Ml8vq2C7exhPAKHd/twpCFomku6FEKt/M8CU1dYFbwzMO3H2rmd1M8J7kj8pqHL7U6SklCqlOdGYhIiKR1GchIiKRlCxERCSSkoWIiERSshARkUhKFiIiEknJQkREIv1/3vl6rblEGOsAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot CV误差曲线\n",
    "test_means = grid.cv_results_[ 'mean_test_score' ]\n",
    "test_stds = grid.cv_results_[ 'std_test_score' ]\n",
    "train_means = grid.cv_results_[ 'mean_train_score' ]\n",
    "train_stds = grid.cv_results_[ 'std_train_score' ]\n",
    "\n",
    "# plot results\n",
    "n_Cs = len(Cs)\n",
    "number_penaltys = len(penaltys)\n",
    "test_scores = np.array(test_means).reshape(n_Cs,number_penaltys)\n",
    "train_scores = np.array(train_means).reshape(n_Cs,number_penaltys)\n",
    "test_stds = np.array(test_stds).reshape(n_Cs,number_penaltys)\n",
    "train_stds = np.array(train_stds).reshape(n_Cs,number_penaltys)\n",
    "\n",
    "x_axis = np.log10(Cs)\n",
    "for i, value in enumerate(penaltys):\n",
    "    #pyplot.plot(log(Cs), test_scores[i], label= 'penalty:'   + str(value))\n",
    "    plt.errorbar(x_axis, -test_scores[:,i], yerr=test_stds[:,i] ,label = penaltys[i] +' Test')\n",
    "    #plt.errorbar(x_axis, -train_scores[:,i], yerr=train_stds[:,i] ,label = penaltys[i] +' Train')\n",
    "    \n",
    "plt.legend()\n",
    "plt.xlabel( 'log(C)' )                                                                                                      \n",
    "plt.ylabel( 'logloss' )\n",
    "plt.savefig('LogisticGridSearchCV_C.png' )\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cPickle\n",
    "\n",
    "cPickle.dump(grid.best_estimator_, open(\"L2_tfidf_logloss.pkl\", 'wb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
